package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.dto.req.PackageManageAddUpdateVo;
import com.yswy.backstage.dto.req.PackageManageListVo;
import com.yswy.backstage.service.AppProjectService;
import com.yswy.backstage.service.PackageManageLogService;
import com.yswy.backstage.service.SsoService;
import com.yswy.common.enums.TfPlatformEnums;
import com.yswy.common.enums.XtPlatformEnums;
import com.yswy.common.enums.base.TypeEnum;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.PackageVersion;
import com.yswy.domain.entity.user.User;
import com.yswy.backstage.service.PackageManageService;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import com.yswy.common.utils.StringUtils;
import com.yswy.common.utils.redis.RedisConstants;
import com.yswy.common.utils.redis.RedisUtil;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.PackageManageMapper;
import com.yswy.domain.vo.gld.addOrUpate.PackageManageAddOrUpdate;
import com.yswy.domain.vo.gld.search.PackageManageSearch;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;


@Slf4j
@Service
public class PackageManageServiceImpl extends ServiceImpl<PackageManageMapper, PackageManage>
        implements PackageManageService {
    @Autowired
    private PackageManageLogService packageManageLogService ;
    @Autowired
    private SsoService ssoService;
    @Override
    public Page<PackageManage> pagePackageManage(PackageManageSearch packageManageSearch ) {

        if (StringUtils.isNotEmpty(packageManageSearch.getXtPlatform())){
            packageManageSearch.setXtPlatform(TypeEnum.messageOf(XtPlatformEnums.class , packageManageSearch.getXtPlatform()).toString());
        }
        if (StringUtils.isNotEmpty(packageManageSearch.getTfPlatform())){
            packageManageSearch.setTfPlatform(TypeEnum.messageOf(TfPlatformEnums.class , packageManageSearch.getTfPlatform()).toString());
        }

        Page<PackageManage> pagePackageManages = baseMapper.pagePackageManage(packageManageSearch
                ,ssoService.isAdminToUserId(CurrentUserContext.getCurrentUser())
                , new Page<>(packageManageSearch.getPageNum() , packageManageSearch.getPageSize()) );
        for (PackageManage packageManage:pagePackageManages.getRecords()){
            TypeEnum xtPlatformEnum = TypeEnum.typeEnumOf(XtPlatformEnums.class , packageManage.getXtPlatform());
            packageManage.setXtPlatform(xtPlatformEnum.getMessage());
            TypeEnum tfPlatformEnum = TypeEnum.typeEnumOf(TfPlatformEnums.class , packageManage.getTfPlatform());
            packageManage.setTfPlatform(tfPlatformEnum.getMessage());
        }

        return pagePackageManages;
    }

    @Override
    public List<PackageManage> listPackageManage(Long packageManageId) {
        List<PackageManage> listPackageManages = baseMapper.listPackageManage(
                ssoService.isAdminToUserId(CurrentUserContext.getCurrentUser())
                ,packageManageId
                );
        for (PackageManage packageManage:listPackageManages){
            TypeEnum xtPlatformEnum = TypeEnum.typeEnumOf(XtPlatformEnums.class , packageManage.getXtPlatform());
            packageManage.setXtPlatform(xtPlatformEnum.getMessage());
            TypeEnum tfPlatformEnum = TypeEnum.typeEnumOf(TfPlatformEnums.class , packageManage.getTfPlatform());
            packageManage.setTfPlatform(tfPlatformEnum.getMessage());
        }
        return listPackageManages;
    }

    @Override
    public void addOrUpdate(PackageManageAddOrUpdate res) {
        User user = CurrentUserContext.getCurrentUser();
        Boolean isExistchannel =  isExistchannel(res.getProjectId(), res.getChannel());

        if (res.getId() != null){
            //更新数据
            PackageManage bo = getById(res.getId());
            Assert.state(null != bo && null != bo.getId(), NULL_MSG);
            BeanUtil.copyProperties(res, bo);
            BasePojo.setBase(user.getId().longValue(), false, bo);
            updateById(bo);
        }else {
            if (isExistchannel){
                throw new IllegalStateException("渠道已存在");
            }
            PackageManage packageManage = new PackageManage(res , user);

            save(packageManage);
            //自动打包
            packageManageLogService.newPackageManageLog(packageManage);
        }
    }

    @Override
    public Boolean isExistchannel(Long projectId, String channel) {
        Integer count = count(new LambdaQueryWrapper<PackageManage>()
                .eq(PackageManage::getChannel , channel)
                .eq(PackageManage::getProjectId , projectId)
                .eq(BasePojo::getIsDel , 0));
        if (count>0){
            return true ;
        }

        return false;
    }

    @Override
    public void delPackageManage(Long packageManageId ) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, packageManageId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        PackageManage bo = new PackageManage();
        bo.setId(packageManageId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);

    }
}
